<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>IteratorAggregate（聚合式迭代器）接口</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="iterator.valid.html">« Iterator::valid</a></li>
      <li style="float: right;"><a href="iteratoraggregate.getiterator.html">IteratorAggregate::getIterator »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="reserved.interfaces.html">预定义接口和类</a></li>
    <li>IteratorAggregate（聚合式迭代器）接口</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="class.iteratoraggregate" class="reference">

 <h1 class="title">IteratorAggregate（聚合式迭代器）接口</h1>
 

 <div class="partintro"><p class="verinfo">(PHP 5, PHP 7, PHP 8)</p>


  <div class="section" id="iteratoraggregate.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    创建外部迭代器的接口。
   </p>
  </div>


  <div class="section" id="iteratoraggregate.synopsis">
   <h2 class="title">接口摘要</h2>


   <div class="classsynopsis">
    <div class="ooclass">
     
    </div>

    <div class="classsynopsisinfo">
     <span class="oointerface">
      <span class="modifier">interface</span> <strong class="interfacename">IteratorAggregate</strong></span><span class="ooclass">
      <span class="modifier">extends</span>
       <a href="class.traversable.html" class="classname">Traversable</a>
     </span>
     {</div>
    
    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="iteratoraggregate.getiterator.html" class="methodname">getIterator</a></span>(): <span class="type"><a href="class.traversable.html" class="type Traversable">Traversable</a></span></div>

   }</div>


  </div>

  <div class="section" id="iteratoraggregate.examples">
   <div class="example" id="iteratoraggregate.example.basic">
    <p><strong>示例 #1 基本用法</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">myData&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">IteratorAggregate&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$property1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Public&nbsp;property&nbsp;one"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$property2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Public&nbsp;property&nbsp;two"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$property3&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Public&nbsp;property&nbsp;three"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">property4&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"last&nbsp;property"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">getIterator</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;new&nbsp;</span><span style="color: #0000BB">ArrayIterator</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">myData</span><span style="color: #007700">;<br /><br />foreach(</span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(9) &quot;property1&quot;
string(19) &quot;Public property one&quot;

string(9) &quot;property2&quot;
string(19) &quot;Public property two&quot;

string(9) &quot;property3&quot;
string(21) &quot;Public property three&quot;

string(9) &quot;property4&quot;
string(13) &quot;last property&quot;

</pre></div>
    </div>
   </div>
  </div>


 </div>

 








<h2>目录</h2><ul class="chunklist chunklist_reference"><li><a href="iteratoraggregate.getiterator.html">IteratorAggregate::getIterator</a> — 获取一个外部迭代器</li></ul>
</div>
</div></div></body></html>